﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;
using System.Configuration;

public partial class admin_AfficheInformSetting_UpdateAfficheInformInfo : System.Web.UI.Page {

    private int ai_id;

    protected void Page_Load(object sender, EventArgs e) {
        if(!Page.IsPostBack) {
            if(Request.QueryString.Get("ai_id") != null) {
                this.TxtEnd.Attributes.Add("onfocus", "calendar()");
                this.TxtStart.Attributes.Add("onfocus", "calendar()");
                AfficheInformtbService aiService = new AfficheInformtbService();
                AfficheInformtb ai = aiService.findById(int.Parse(Request.QueryString.Get("ai_id")));
                if(ai != null) {
                    this.TxtTitle.Text = ai.AfficheInformTitle;
                    this.MyEditor.value = ai.AfficheInformContent;
                    this.TxtSections.Text = ai.AfficheInformOpenBoundSnames.Replace("<br />", ",");
                    this.TxtStart.Text = ai.AfficheInformStartTime.ToShortDateString();
                    this.TxtEnd.Text = ai.AfficheInformEndTime.ToShortDateString();
                    this.HidSids.Value = ai.AfficheInformOpenBound;
                    if(ai.AfficheInformAffixFilePath != null && !"".Equals(ai.AfficheInformAffixFilePath)) {
                        string[] aiAps = ai.AfficheInformAffixFilePath.Split(',');
                        for(int i = 0; i < aiAps.Length; i++) {
                            string aiName = aiAps[i].Substring(aiAps[i].IndexOf("_") + 1);
                            this.ListBox1.Items.Add(new ListItem(aiName, aiAps[i]));
                        }
                    }
                }
                ViewState["ai_id"] = Request.QueryString.Get("ai_id");
            }
        }
    }

    protected void upload() {
        string rootPath = ConfigurationManager.AppSettings["AfficheInformAffixFileRoot"];
        string fileName = this.AffixFile.FileName;
        string prefix = DateTime.Now.Ticks.ToString();
        AffixFile.SaveAs(rootPath + "\\" + prefix + "_" + fileName);
    }

    protected void BtnUpload_Click(object sender, EventArgs e) {
        if(this.AffixFile.HasFile) {
            string rootPath = ConfigurationManager.AppSettings["AfficheInformAffixFileRoot"];
            DirectoryInfo di = new DirectoryInfo(rootPath);
            if(!di.Exists) {
                di.Create();
            }
            string fileName = this.AffixFile.FileName;
            string prefix = DateTime.Now.Ticks.ToString();
            Thread t = new Thread(new ThreadStart(upload));
            t.Start();
            this.ListBox1.Items.Add(new ListItem(fileName, prefix + "_" + fileName));
        }
    }

    protected void BtnSubmit_Click(object sender, EventArgs e) {
        if(ViewState["ai_id"] != null){
            ai_id = int.Parse((string)ViewState["ai_id"]);
        }
        string sids = "," + Request.Form.Get("HidSids");
        string aiTitle = Request.Form.Get(this.TxtTitle.ClientID);
        DateTime start = DateTime.Parse(Request.Form.Get(this.TxtStart.ClientID));
        DateTime end = DateTime.Parse(Request.Form.Get(this.TxtEnd.ClientID));
        string aiContent = Request.Form.Get(this.MyEditor.ClientID);
        string[] affixFiles = Request.Form.GetValues(this.ListBox1.ClientID);
        string affixFilePaths = "";
        if(affixFiles != null) {
            for(int i = 0; i < affixFiles.Length; i++) {
                if(i == affixFiles.Length - 1) {
                    affixFilePaths += affixFiles[i];
                } else {
                    affixFilePaths += (affixFiles[i] + ",");
                }
            }
        }
        //User curLoginUser = (User) Session["user"];
        AfficheInformtbService aiService = new AfficheInformtbService();
        if(aiService.updateAfficheInformInfo(aiTitle,aiContent,DateTime.Now, start, end, affixFilePaths, sids, ai_id) == 1) {
            Response.Redirect("Index.aspx");
        }
    }

    protected void BtnDelFile_Click(object sender, EventArgs e) {
        string[] selectFiles = Request.Form.GetValues(this.ListBox1.ClientID);
        if(selectFiles != null) {
            for(int i = 0; i < selectFiles.Length; i++) {
                string filepath = ConfigurationManager.AppSettings["AfficheInformAffixFileRoot"] + "\\" + selectFiles[i];
                FileInfo fi = new FileInfo(filepath);
                if(fi.Exists) {
                    fi.Delete();
                    for(int j = 0; j < this.ListBox1.Items.Count; j++) {
                        if(ListBox1.Items[j].Value.Equals(selectFiles[i])) {
                            ListBox1.Items.Remove(ListBox1.Items[j]);
                            j--;
                        }
                    }
                }
            }
        }
    }

}
